package com.zlkj.manager.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.zlkj.manager.domain.MinioFile;
import com.zlkj.manager.domain.R;
import com.zlkj.manager.service.ISysFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.zlkj.manager.domain.SysOss;
import com.zlkj.manager.service.ISysOssService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import static com.zlkj.manager.utils.FileUploadUtils.getExtension;

/**
 * 文件存储Controller
 * 
 * @author liubo
 * @date 2022-03-07
 */
@RestController
@RequestMapping("/manager/file")
public class SysOssController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(SysOssController.class);

    @Autowired
    private ISysOssService sysOssService;

    @Autowired
    private ISysFileService sysFileService;
    /**
     * 查询文件存储列表
     */
    @PreAuthorize("@ss.hasPermi('manager:oss:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOss sysOss)
    {
        startPage();
        List<SysOss> list = sysOssService.selectSysOssList(sysOss);
        return getDataTable(list);
    }

    /**
     * 导出文件存储列表
     */
    @PreAuthorize("@ss.hasPermi('manager:oss:export')")
    @Log(title = "文件存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOss sysOss)
    {
        List<SysOss> list = sysOssService.selectSysOssList(sysOss);
        ExcelUtil<SysOss> util = new ExcelUtil<SysOss>(SysOss.class);
        util.exportExcel(response, list, "文件存储数据");
    }

    /**
     * 获取文件存储详细信息
     */
    @PreAuthorize("@ss.hasPermi('manager:oss:query')")
    @GetMapping(value = "/{ossId}")
    public AjaxResult getInfo(@PathVariable("ossId") Long ossId)
    {
        return AjaxResult.success(sysOssService.selectSysOssByOssId(ossId));
    }

    /**
     * 新增文件存储
     */
    @PreAuthorize("@ss.hasPermi('manager:oss:add')")
    @Log(title = "文件存储", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysOss sysOss)
    {
        return toAjax(sysOssService.insertSysOss(sysOss));
    }

    /**
     * 修改文件存储
     */
    @PreAuthorize("@ss.hasPermi('manager:oss:edit')")
    @Log(title = "文件存储", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysOss sysOss)
    {
        return toAjax(sysOssService.updateSysOss(sysOss));
    }

    /**
     * 删除文件存储
     */
    @PreAuthorize("@ss.hasPermi('manager:oss:remove')")
    @Log(title = "文件存储", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ossIds}")
    public AjaxResult remove(@PathVariable Long[] ossIds)
    {
        return toAjax(sysOssService.deleteSysOssByOssIds(ossIds));
    }




    /**
     * 文件上传请求
     */
    @PostMapping("/upload")
    public R<MinioFile> upload(MultipartFile file)
    {
        try
        {
            // 上传并返回访问地址
            String url = sysFileService.uploadFile(file);
            MinioFile minioFile = new MinioFile();
            minioFile.setName(getName(url));
            minioFile.setUrl(url);
            SysOss sysOss = new SysOss();
            sysOss.setFileName(minioFile.getName());
            sysOss.setFileType("照片");
            sysOss.setOriginalName(file.getOriginalFilename());
            sysOss.setFileSuffix(getExtension(file));
            sysOss.setUrl(minioFile.getUrl());
            sysOssService.insertSysOss(sysOss);
            return R.ok(minioFile,"上传成功 地址为: " + minioFile.getUrl());
        }
        catch (Exception e)
        {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }


    /**
     * 获取名称
     *
     * @param fileName 路径名称
     * @return 没有文件路径的名称
     */
    public static String getName(String fileName)
    {
        if (fileName == null)
        {
            return null;
        }
        int lastUnixPos = fileName.lastIndexOf('/');
        int lastWindowsPos = fileName.lastIndexOf('\\');
        int index = Math.max(lastUnixPos, lastWindowsPos);
        return fileName.substring(index + 1);
    }

}
